//
//  MessageReceiveViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/3.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "MessageReceiveViewController.h"
#import "HeaderFileForEasyToTesco.h"
#import "AFNetworking.h"


@interface MessageReceiveViewController ()<UITextFieldDelegate>

@end

@implementation MessageReceiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self addmessagereceiveTopView];
    
}


- (void)addmessagereceiveTopView {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"意见反馈";
    
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WEITH-100, 5, 80, CGRectGetHeight(self.secondView.frame))];
    title.text = @"提交发送";
    title.textAlignment = NSTextAlignmentRight;
    title.textColor = [UIColor whiteColor];
    title.font = [UIFont systemFontOfSize:14];
    [self.secondView addSubview:title];
    
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.frame = CGRectMake(WEITH-100, 0, 100, CGRectGetHeight(self.secondView.frame));
    [doneBtn addTarget:self action:@selector(doneBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:doneBtn];
    
    //添加触摸手势，当点击View上其他地方时会隐藏键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignResponders)];
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tap];
}

- (void)resignResponders {
    
    [self.messagecontenttext resignFirstResponder];
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)doneBtnClick{
    if (self.messagecontenttext.text.length != 0) {
        [self messageContentdatasources];
    } else {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"" message:@"对不起,您还没与输入内用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil , nil];
        [alview show];
    }
}


- (void)messageContentdatasources {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = @"15937728275";
    NSString *content = self.messagecontenttext.text;
    NSString *phone = @"15937728275";

    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username], @"content":[NSString stringWithFormat:@"%@", content], @"phone":[NSString stringWithFormat:@"%@", phone]};
    
    [mgr POST:@"http://www.ylgw8.com/AppPub/Feedback" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
        
        NSLog(@"%@", dict[@"message"]);
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"%@", dict[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil , nil];
        [alview show];
    
      
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
